<?php

namespace App\Http\Controllers\News;

use App\Model\Classify;
use App\Model\News;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
            $news=News::all();
            $classify= $classify = (new Classify())->getTree();
        return view('news.news.index',compact('news','classify'));
    }
    public function create()
    {
        $classify = (new Classify())->getTree();
//        dd($classify);
        return view('news.news.create', compact('classify'));
    }
    public function store(Request $request)
    {
       $request['editormd']=$request['editormd-html-code'];
//       dd($request->toArray());
        $request->validate([
            'title' => 'required',
            'brief'=>'required',
            'icon'=>'required',
            'classify_id' => 'required',
            'content' => 'required'
        ], [
            'title.required' => '新闻标题不为空',
            'brief.required' => '新闻简介不为空',
            'icon.required' => '新闻缩略图不为空',
            'classify_id.required' => '新闻分类不为空',
            'content.required' => '新闻内容不为空'
        ]);
        auth()->user()->news()->create($request->all());
//        $request['auth']=auth()->user()->name;
//        News::create($request->all());
        return back();
//        return redirect()->route('news.news.index')->with('success', '添加新闻成功');
    }

    public function show(News $news)
    {
        $classify= $classify = (new Classify())->getTree();
        return view('news.news.show',compact('news','classify'));
    }
    public function edit(News $news)
    {
         $classify = (new Classify())->getTree();

        return view('news.news.edit', compact('classify', 'news'));
    }
    public function update(Request $request, News $news)
    {

        $request->validate([
            'title' => 'required',
            'brief'=>'required',
            'icon'=>'required',
            'classify_id' => 'required',
            'content' => 'required'
        ], [
            'title.required' => '新闻标题不为空',
            'brief.required' => '新闻简介不为空',
            'icon.required' => '新闻缩略图不为空',
            'classify_id.required' => '新闻分类不为空',
            'content.required' => '新闻内容不为空'
        ]);
        $news->update($request->all());
        return redirect()->route('news.lists')->with('success', '编辑成功');
    }
    public function destroy(News $news)
    {
        $news->delete();
        return back();
    }
    public function lists()
    {
        $news = News::all();
        return view('news.news.lists', compact('news'));
    }
//    public function classify(Classify $classify){
//
//        return view('news.news.classify',compact('classify'));
//    }
}
